#coding=utf-8
import urllib,urllib2
from BeautifulSoup import BeautifulSoup
import re

def en2cn(text):
    values={'hl':'zh-CN','ie':'utf8','text':text,'langpair':"en|zh-CN"}
    url='http://translate.google.cn/translate_t'
    data = urllib.urlencode(values)
    req = urllib2.Request(url, data)
    req.add_header('User-Agent', "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)")
    response = urllib2.urlopen(req) 
    
    html_soup = BeautifulSoup(''.join(response.read()))
    
    html_need = html_soup.findAll("span",{"id":"result_box"})
    #print html_need[0]
    #html_content = html_need[0].findAll("div",{"id":"src-translit"})
    #print html_content
    html_final = html_need[0].findAll('span')
    
    return html_final[0].string
#end def

def cn2en(text):
    values={'hl':'zh-CN','ie':'utf-8','text':text,'langpair':"zh-CN|en"}
    url='http://translate.google.cn/translate_t'
    data = urllib.urlencode(values)
    req = urllib2.Request(url, data)
    req.add_header('User-Agent', "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)")
    response = urllib2.urlopen(req) 
    
    html_soup = BeautifulSoup(''.join(response.read()))
    
    html_need = html_soup.findAll("span",{"id":"result_box"})
    #print html_need[0]
    #html_content = html_need[0].findAll("div",{"id":"src-translit"})
    #print html_content
    html_final = html_need[0].findAll('span')
    
    return unicode(html_final[0].string.decode("utf8"))
#end def

print type(cn2en("如何获得系统的默认编码 ？"))
print type(unicode(en2cn("Beautiful soup")))